﻿using JinYuan.Helper;
using JinYuan.VirtualDataLibrary;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace LargeSquareOne
{
    public partial class FrmStaticsQuery : Form
    {

        /// <summary>
        /// 班次
        /// </summary>
        private DataTable dtClasses = null;
        /// <summary>
        /// 日
        /// </summary>
        private DataTable dtDays = null;
        /// <summary>
        /// 周
        /// </summary>
        private DataTable dtWeeks = null;
        /// <summary>
        /// 月
        /// </summary>
        private DataTable dtMonths = null;
        public FrmStaticsQuery()
        {
            InitializeComponent();
        }

        private void FrmStaticsQuery_Load(object sender, EventArgs e)
        {
            //获取当前日期
            string startTimeStr = dtpDays.Value.ToString("yyyy-MM-dd 00:00:00");
            string endTimeStr = dtpDays.Value.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59");

            //获取月份
            int NowMonth = DateTime.Now.Month;
            for (int i = 0; i < NowMonth; i++)
            {
                cmbYears.Items.Add(i + 1);
            }
            cmbYears.SelectedIndex = 0;

            //班次
            dtClass.Value = DateTime.Now.Date;
            cmbClass.SelectedIndex = 0;
        }

        #region 按班次
        /// <summary>
        /// 按班次查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ClassQuery_Click(object sender, EventArgs e)
        {
            DateTime strDate = dtClass.Value;
            string strClass = cmbClass.Text;
            dtClasses = CommonMethods.db.QueryDataTable(CommonMethods.dBSQL.GetStaticByShift(strDate, strClass, CommonMethods.ChangeClassTime));
            dgvClass.DataSource = null;
            if (dtClasses != null && dtClasses.Rows.Count > 0)
            {
                dgvClass.DataSource = dtClasses;
            }
        }

        /// <summary>
        /// 按班次导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ClassExPort_Click(object sender, EventArgs e)
        {
            if (dgvClass.Columns.Count <= 0)
            {
                MessageBox.Show("导出失败，无查询结果");
                return;
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.FileName = $"产能统计_按班次统计_{DateTime.Now.ToString("yyyyMMddHHmmss")}";
            saveFileDialog.Filter = "*|*.csv";
            if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
                return;

            string msg = "";
            if (CSVHelper<object>.ExportDataTableToCSV(dtClasses, saveFileDialog.FileName, out msg))
            {
                MessageBox.Show("导出成功!");
            }
        }
        #endregion

        #region 按日
        /// <summary>
        /// 按日统计查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DaysQuery_Click(object sender, EventArgs e)
        {
            string startDateTime = dtpDays.Value.ToString("yyyy-MM-dd 00:00:00");
            string endDateTime = dtpDays.Value.ToString("yyyy-MM-dd 23:59:59");
            dtDays = CommonMethods.db.QueryDataTable(CommonMethods.dBSQL.GetStaticByDay(startDateTime, endDateTime));
            dgvDays.DataSource = null;
            if (dtDays != null && dtDays.Rows.Count > 0)
            {
                dgvDays.DataSource = dtDays;
            }
        }

        /// <summary>
        /// 按日导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DaysExPort_Click(object sender, EventArgs e)
        {
            if (dgvDays.Columns.Count <= 0)
            {
                MessageBox.Show("导出失败，无查询结果");
                return;
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.FileName = $"产能统计_按日统计_{DateTime.Now.ToString("yyyyMMddHHmmss")}";
            saveFileDialog.Filter = "*|*.csv";
            if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
                return;

            string msg = "";
            if (CSVHelper<object>.ExportDataTableToCSV(dtDays, saveFileDialog.FileName, out msg))
            {
                MessageBox.Show("导出成功!");
            }
        }
        #endregion

        #region 按周
        /// <summary>
        /// 按周统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_WeekQuery_Click(object sender, EventArgs e)
        {
            //确定星期几
            var index = (int)dtpWeek.Value.DayOfWeek;
            index = index == 0 ? 7 : index;
            //当前周的范围
            string retStartDay = dtpWeek.Value.AddDays(-(index - 1)).ToString();
            string retEndDay = dtpWeek.Value.AddDays(7 - index).ToString();
            dtWeeks = CommonMethods.db.QueryDataTable(CommonMethods.dBSQL.GetStaticByWeek(retStartDay, retEndDay));
            dgvWeek.DataSource = null;
            if (dtWeeks != null && dtWeeks.Rows.Count > 0)
            {
                dgvWeek.DataSource = dtWeeks;
            }
        }

        /// <summary>
        /// 按周导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_WeekExPort_Click(object sender, EventArgs e)
        {
            if (dgvWeek.Columns.Count <= 0)
            {
                MessageBox.Show("导出失败，无查询结果");
                return;
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.FileName = $"产能统计_按周统计_{DateTime.Now.ToString("yyyyMMddHHmmss")}";
            saveFileDialog.Filter = "*|*.csv";
            if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
                return;

            string msg = "";
            if (CSVHelper<object>.ExportDataTableToCSV(dtWeeks, saveFileDialog.FileName, out msg))
            {
                MessageBox.Show("导出成功!");
            }
        }
        #endregion

        #region 按月
        /// <summary>
        /// 按月查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_MonthQuery_Click(object sender, EventArgs e)
        {
            string startDateTime = dtMonth.Value.ToString("yyyy-MM-01 00:00:00");

            string endDateTime = new DateTime(dtMonth.Value.Year, dtMonth.Value.Month, DateTime.DaysInMonth(dtMonth.Value.Year, dtMonth.Value.Month)).ToString("yyyy-MM-dd 23:59:59");//通过指定日期计算当月最后一天

            //string endDateTime = Convert.ToDateTime(startDateTime).AddDays(12).ToString("yyyy-MM-dd 23:59:59");
            //string endDateTime = dtMonths.Value.ToString("yyyy-MM-dd 23:59:59");

            dtMonths = CommonMethods.db.QueryDataTable(CommonMethods.dBSQL.GetStaticByMonth(startDateTime, endDateTime));
            dgvMonth.DataSource = null;
            if (dtMonth != null && dtMonths.Rows.Count > 0)
            {
                dgvMonth.DataSource = dtMonths;
            }
        }

        /// <summary>
        /// 按月查询导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_MonthExPort_Click(object sender, EventArgs e)
        {
            if (dgvMonth.Columns.Count <= 0)
            {
                MessageBox.Show("导出失败，无查询结果");
                return;
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.FileName = $"产能统计_按月统计_{DateTime.Now.ToString("yyyyMMddHHmmss")}";
            saveFileDialog.Filter = "*|*.csv";
            if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
                return;

            string msg = "";
            if (CSVHelper<object>.ExportDataTableToCSV(dtMonths, saveFileDialog.FileName, out msg))
            {
                MessageBox.Show("导出成功!");
            }
        }
        #endregion

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 窗体拖动
        public static Point CPoint;

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            CPoint = new Point(-e.X, -e.Y);
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
                myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
                this.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
            }
        }
        #endregion

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion
    }
}
